import { test, expect } from "@playwright/test";
import { loginAs } from "../utils/auth";
import {
  TOOL_IDS,
  waitForUnifiedGreeting,
  openActionManagement,
} from "../utils/tools";
import { OnyxApiClient } from "../utils/onyxApiClient";

test.describe("Default Assistant Admin Page", () => {
  let testCcPairId: number | null = null;
  let webSearchProviderId: number | null = null;
  let imageGenProviderId: number | null = null;

  test.beforeEach(async ({ page }) => {
    // Log in as admin
    await page.context().clearCookies();
    await loginAs(page, "admin");

    const apiClient = new OnyxApiClient(page);

    // Create a connector so Internal Search tool becomes available
    testCcPairId = await apiClient.createFileConnector(
      `Test Connector ${Date.now()}`
    );

    // Create providers for Web Search and Image Generation tools
    try {
      webSearchProviderId = await apiClient.createWebSearchProvider(
        "exa",
        `Test Web Search Provider ${Date.now()}`
      );
      imageGenProviderId = await apiClient.createImageGenProvider(
        `Test Image Gen Provider ${Date.now()}`
      );
    } catch (error) {
      console.warn(`Failed to create tool providers: ${error}`);
    }

    // Navigate to default assistant
    await page.goto(
      "http://localhost:3000/admin/configuration/default-assistant"
    );
    await page.waitForURL(
      "http://localhost:3000/admin/configuration/default-assistant"
    );

    // Attach basic API logging for this spec
    page.on("response", async (resp) => {
      const url = resp.url();
      if (url.includes("/api/admin/default-assistant")) {
        const method = resp.request().method();
        const status = resp.status();
        let body = "";
        try {
          body = await resp.text();
        } catch {}
        console.log(
          `[api:response] ${method} ${url} => ${status} body=${body?.slice(
            0,
            300
          )}`
        );
      }
    });

    // Proactively log tool availability and current config
    try {
      const toolsResp = await page.request.get(
        "http://localhost:3000/api/tool"
      );
      const cfgResp = await page.request.get(
        "http://localhost:3000/api/admin/default-assistant/configuration"
      );
      console.log(
        `[/api/tool] status=${toolsResp.status()} body=${(
          await toolsResp.text()
        ).slice(0, 400)}`
      );
      console.log(
        `[/configuration] status=${cfgResp.status()} body=${(
          await cfgResp.text()
        ).slice(0, 400)}`
      );
    } catch (e) {
      console.log(`[setup] Failed to fetch initial admin config: ${String(e)}`);
    }
  });

  test.afterEach(async ({ page }) => {
    const apiClient = new OnyxApiClient(page);

    // Clean up the test connector
    if (testCcPairId !== null) {
      try {
        await apiClient.deleteCCPair(testCcPairId);
        testCcPairId = null;
      } catch (error) {
        console.warn(
          `Failed to delete test connector ${testCcPairId}: ${error}`
        );
      }
    }

    // Clean up web search provider
    if (webSearchProviderId !== null) {
      try {
        await apiClient.deleteWebSearchProvider(webSearchProviderId);
        webSearchProviderId = null;
      } catch (error) {
        console.warn(
          `Failed to delete web search provider ${webSearchProviderId}: ${error}`
        );
      }
    }

    // Clean up image gen provider
    if (imageGenProviderId !== null) {
      try {
        await apiClient.deleteProvider(imageGenProviderId);
        imageGenProviderId = null;
      } catch (error) {
        console.warn(
          `Failed to delete image gen provider ${imageGenProviderId}: ${error}`
        );
      }
    }
  });

  test("should load default assistant page for admin users", async ({
    page,
  }) => {
    // Verify page loads with expected content
    await expect(page.locator('[aria-label="admin-page-title"]')).toHaveText(
      "Default Assistant"
    );
    // Avoid strict mode collision from multiple "Actions" elements
    await expect(page.getByText("Instructions", { exact: true })).toBeVisible();
    await expect(page.getByText("Instructions", { exact: true })).toBeVisible();
  });

  test("should toggle Internal Search tool on and off", async ({ page }) => {
    await page.waitForSelector("text=Internal Search", { timeout: 10000 });

    // Find the Internal Search checkbox using a more robust selector
    const searchCheckbox = page.getByLabel("internal-search-checkbox").first();

    // Get initial state
    const initialState = await searchCheckbox.getAttribute("aria-checked");
    const isDisabled = initialState === "false";
    console.log(
      `[toggle] Internal Search initial data-state=${initialState} disabled=${isDisabled}`
    );

    // Toggle it
    await searchCheckbox.click();
    await page.waitForTimeout(500);

    // Save changes
    const saveButton = page.getByRole("button", { name: "Save Changes" });
    await expect(saveButton).toBeVisible({ timeout: 5000 });
    await saveButton.click();

    // Wait for PATCH to complete
    const patchResp = await page.waitForResponse(
      (r) =>
        r.url().includes("/api/admin/default-assistant") &&
        r.request().method() === "PATCH",
      { timeout: 8000 }
    );
    console.log(
      `[toggle] Internal Search PATCH status=${patchResp.status()} body=${(
        await patchResp.text()
      ).slice(0, 300)}`
    );

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
    await page.waitForTimeout(500);

    // Refresh page to verify persistence
    await page.reload();
    await page.waitForSelector("text=Internal Search", { timeout: 10000 });

    const newState = await searchCheckbox.getAttribute("aria-checked");
    console.log(`[toggle] Internal Search after reload data-state=${newState}`);

    // State should have changed
    expect(initialState).not.toBe(newState);

    // Toggle back to original state
    await searchCheckbox.click();
    await page.waitForTimeout(500);

    // Save the restoration
    const saveButtonRestore = page.getByRole("button", {
      name: "Save Changes",
    });
    await expect(saveButtonRestore).toBeVisible({ timeout: 5000 });
    await saveButtonRestore.click();
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
  });

  test("should toggle Web Search tool on and off", async ({ page }) => {
    await page.waitForSelector("text=Web Search", { timeout: 10000 });

    // Find the Web Search checkbox using a more robust selector
    const webSearchCheckbox = page.getByLabel("web-search-checkbox").first();

    // Get initial state
    const initialState = await webSearchCheckbox.getAttribute("aria-checked");
    const isDisabled = initialState === "false";
    console.log(
      `[toggle] Web Search initial data-state=${initialState} disabled=${isDisabled}`
    );

    // Toggle it
    await webSearchCheckbox.click();
    await page.waitForTimeout(500);

    // Save changes
    const saveButton = page.getByRole("button", { name: "Save Changes" });
    await expect(saveButton).toBeVisible({ timeout: 5000 });
    await saveButton.click();

    // Wait for PATCH to complete
    const patchResp = await page.waitForResponse(
      (r) =>
        r.url().includes("/api/admin/default-assistant") &&
        r.request().method() === "PATCH",
      { timeout: 8000 }
    );
    console.log(
      `[toggle] Web Search PATCH status=${patchResp.status()} body=${(
        await patchResp.text()
      ).slice(0, 300)}`
    );

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
    await page.waitForTimeout(500);

    // Refresh page to verify persistence
    await page.reload();
    await page.waitForSelector("text=Web Search", { timeout: 10000 });

    // Check that state persisted
    const newState = await webSearchCheckbox.getAttribute("aria-checked");
    console.log(`[toggle] Web Search after reload data-state=${newState}`);

    // State should have changed
    expect(initialState).not.toBe(newState);

    // Toggle back to original state
    await webSearchCheckbox.click();
    await page.waitForTimeout(500);

    // Save the restoration
    const saveButtonRestore = page.getByRole("button", {
      name: "Save Changes",
    });
    await expect(saveButtonRestore).toBeVisible({ timeout: 5000 });
    await saveButtonRestore.click();
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
  });

  test("should toggle Image Generation tool on and off", async ({ page }) => {
    await page.waitForSelector("text=Image Generation", { timeout: 10000 });

    // Find the Image Generation checkbox using a more robust selector
    const imageGenCheckbox = page
      .getByLabel("image-generation-checkbox")
      .first();

    // Get initial state
    const initialState = await imageGenCheckbox.getAttribute("aria-checked");
    const isDisabled = initialState === "false";
    console.log(
      `[toggle] Image Generation initial data-state=${initialState} disabled=${isDisabled}`
    );

    // Toggle it
    await imageGenCheckbox.click();
    await page.waitForTimeout(500);

    // Save changes
    const saveButton = page.getByRole("button", { name: "Save Changes" });
    await expect(saveButton).toBeVisible({ timeout: 5000 });
    await saveButton.click();

    // Wait for PATCH to complete
    const patchResp = await page.waitForResponse(
      (r) =>
        r.url().includes("/api/admin/default-assistant") &&
        r.request().method() === "PATCH",
      { timeout: 8000 }
    );
    console.log(
      `[toggle] Image Generation PATCH status=${patchResp.status()} body=${(
        await patchResp.text()
      ).slice(0, 300)}`
    );

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
    await page.waitForTimeout(500);

    // Refresh page to verify persistence
    await page.reload();
    await page.waitForSelector("text=Image Generation", { timeout: 10000 });

    // Check that state persisted
    const newState = await imageGenCheckbox.getAttribute("aria-checked");
    console.log(
      `[toggle] Image Generation after reload data-state=${newState}`
    );

    // State should have changed
    expect(initialState).not.toBe(newState);

    // Toggle back to original state
    await imageGenCheckbox.click();
    await page.waitForTimeout(500);

    // Save the restoration
    const saveButtonRestore = page.getByRole("button", {
      name: "Save Changes",
    });
    await expect(saveButtonRestore).toBeVisible({ timeout: 5000 });
    await saveButtonRestore.click();
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
  });

  test("should edit and save system prompt", async ({ page }) => {
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Find the textarea using a more flexible selector
    const textarea = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );

    // Get initial value
    const initialValue = await textarea.inputValue();

    // Clear and enter new text with random suffix to ensure uniqueness
    const testPrompt = `This is a test system prompt for the E2E test. ${Math.floor(
      Math.random() * 1000000
    )}`;
    await textarea.fill(testPrompt);

    // Save changes
    const saveButton = page.locator("text=Save Changes");
    await saveButton.click();
    const patchResp = await Promise.race([
      page.waitForResponse(
        (r) =>
          r.url().includes("/api/admin/default-assistant") &&
          r.request().method() === "PATCH",
        { timeout: 8000 }
      ),
      page.waitForTimeout(8500).then(() => null),
    ]);
    if (patchResp) {
      console.log(
        `[prompt] Save PATCH status=${patchResp.status()} body=${(
          await patchResp.text()
        ).slice(0, 300)}`
      );
    } else {
      console.log(`[prompt] Did not observe PATCH response on save`);
    }

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });

    // Refresh page to verify persistence
    await page.reload();
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Check that new value persisted
    const textareaAfter = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );
    await expect(textareaAfter).toHaveValue(testPrompt);

    // Restore original value
    await textareaAfter.fill(initialValue);
    const saveButtonAfter = page.locator("text=Save Changes");
    await saveButtonAfter.click();
    await expect(page.getByText(/successfully/i)).toBeVisible();
  });

  test("should allow empty system prompt", async ({ page }) => {
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Find the textarea using a more flexible selector
    const textarea = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );

    // Get initial value to restore later
    const initialValue = await textarea.inputValue();

    // If already empty, add some text first
    if (initialValue === "") {
      await textarea.fill("Temporary text");
      const tempSaveButton = page.locator("text=Save Changes");
      await tempSaveButton.click();
      const patchResp1 = await page.waitForResponse(
        (r) =>
          r.url().includes("/api/admin/default-assistant") &&
          r.request().method() === "PATCH"
      );
      console.log(
        `[prompt-empty] Temp save PATCH status=${patchResp1.status()} body=${(
          await patchResp1.text()
        ).slice(0, 300)}`
      );
      await expect(page.getByText(/successfully/i)).toBeVisible();
      await page.waitForTimeout(1000);
    }

    // Now clear the textarea
    await textarea.fill("");

    // Save changes
    const saveButton = page.locator("text=Save Changes");
    await saveButton.click();
    const patchResp2 = await page.waitForResponse(
      (r) =>
        r.url().includes("/api/admin/default-assistant") &&
        r.request().method() === "PATCH"
    );
    console.log(
      `[prompt-empty] Save empty PATCH status=${patchResp2.status()} body=${(
        await patchResp2.text()
      ).slice(0, 300)}`
    );

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });

    // Refresh page to verify persistence
    await page.reload();
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Check that empty value persisted
    const textareaAfter = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );
    await expect(textareaAfter).toHaveValue("");

    // Restore original value if it wasn't already empty
    if (initialValue !== "") {
      await textareaAfter.fill(initialValue);
      const saveButtonAfter = page.locator("text=Save Changes");
      await saveButtonAfter.click();
      const patchResp3 = await page.waitForResponse(
        (r) =>
          r.url().includes("/api/admin/default-assistant") &&
          r.request().method() === "PATCH"
      );
      console.log(
        `[prompt-empty] Restore PATCH status=${patchResp3.status()} body=${(
          await patchResp3.text()
        ).slice(0, 300)}`
      );
      await expect(page.getByText(/successfully/i)).toBeVisible();
    }
  });

  test("should handle very long system prompt gracefully", async ({ page }) => {
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Find the textarea using a more flexible selector
    const textarea = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );

    // Get initial value to restore later
    const initialValue = await textarea.inputValue();

    // Create a very long prompt (5000 characters)
    const longPrompt = "This is a test. ".repeat(300); // ~4800 characters

    // If the current value is already the long prompt, use a different one
    if (initialValue === longPrompt) {
      const differentPrompt = "Different test. ".repeat(300);
      await textarea.fill(differentPrompt);
    } else {
      await textarea.fill(longPrompt);
    }

    // Save changes
    const saveButton = page.locator("text=Save Changes");
    await saveButton.click();
    const patchResp = await page.waitForResponse(
      (r) =>
        r.url().includes("/api/admin/default-assistant") &&
        r.request().method() === "PATCH"
    );
    console.log(
      `[prompt-long] Save PATCH status=${patchResp.status()} body=${(
        await patchResp.text()
      ).slice(0, 300)}`
    );

    // Wait for success message
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });

    // Verify character count is displayed
    const currentValue = await textarea.inputValue();
    const charCount = page.locator("text=characters");
    await expect(charCount).toContainText(currentValue.length.toString());

    // Restore original value if it's different
    if (initialValue !== currentValue) {
      await textarea.fill(initialValue);
      await saveButton.click();
      const patchRespRestore = await page.waitForResponse(
        (r) =>
          r.url().includes("/api/admin/default-assistant") &&
          r.request().method() === "PATCH"
      );
      console.log(
        `[prompt-long] Restore PATCH status=${patchRespRestore.status()} body=${(
          await patchRespRestore.text()
        ).slice(0, 300)}`
      );
      await expect(page.getByText(/successfully/i)).toBeVisible();
    }
  });

  test("should display character count for system prompt", async ({ page }) => {
    await page.waitForSelector("text=Instructions", { timeout: 10000 });

    // Find the textarea using a more flexible selector
    const textarea = page.locator(
      'textarea[placeholder*="professional email writing assistant"]'
    );

    // Type some text
    const testText = "Test text for character counting";
    await textarea.fill(testText);

    // Check character count is displayed correctly
    await expect(page.locator("text=characters")).toContainText(
      testText.length.toString()
    );
  });

  test("should reject invalid tool IDs via API", async ({ page }) => {
    // Use browser console to send invalid tool IDs
    // This simulates what would happen if someone tried to bypass the UI
    const response = await page.evaluate(async () => {
      const res = await fetch("/api/admin/default-assistant", {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          tool_ids: ["InvalidTool", "AnotherInvalidTool"],
        }),
      });
      return {
        ok: res.ok,
        status: res.status,
        body: await res.text(),
      };
    });
    // Also try via page.request (uses storageState) to capture status in case page fetch fails
    try {
      const alt = await page.request.patch(
        "http://localhost:3000/api/admin/default-assistant",
        {
          data: { tool_ids: ["InvalidTool", "AnotherInvalidTool"] },
          headers: { "Content-Type": "application/json" },
        }
      );
      console.log(
        `[invalid-tools] page.request.patch status=${alt.status()} body=${(
          await alt.text()
        ).slice(0, 300)}`
      );
    } catch (e) {
      console.log(`[invalid-tools] page.request.patch error: ${String(e)}`);
    }

    // Check that the request failed with 400 or 422 (validation error)
    expect(response.ok).toBe(false);
    expect([400, 422].includes(response.status)).toBe(true);
    // The error message should indicate invalid tool IDs
    if (response.status === 400) {
      expect(response.body).toContain("Invalid tool IDs");
    }
  });

  test("should toggle all tools and verify in chat", async ({ page }) => {
    // Providers are now created in beforeEach, so all tools should be available

    // Wait for ALL three tools to be visible in the UI
    await page.waitForSelector("text=Internal Search", { timeout: 10000 });
    await page.waitForSelector("text=Web Search", { timeout: 10000 });
    await page.waitForSelector("text=Image Generation", { timeout: 10000 });

    // Wait for form to fully initialize
    await page.waitForTimeout(2000);

    // Store initial states
    const toolStates: Record<string, string | null> = {};

    // Capture current states (we'll restore these at the end)
    for (const toolName of [
      "Internal Search",
      "Web Search",
      "Image Generation",
    ]) {
      const toolCheckbox = page
        .getByLabel(`${toolName.toLowerCase().replace(" ", "-")}-checkbox`)
        .first();
      const state = await toolCheckbox.getAttribute("aria-checked");
      toolStates[toolName] = state;
      console.log(`[toggle-all] Initial state for ${toolName}: ${state}`);
    }

    // Disable all tools
    for (const toolName of [
      "Internal Search",
      "Web Search",
      "Image Generation",
    ]) {
      const toolCheckbox = page
        .getByLabel(`${toolName.toLowerCase().replace(" ", "-")}-checkbox`)
        .first();
      const currentState = await toolCheckbox.getAttribute("aria-checked");
      if (currentState === "true") {
        await toolCheckbox.click();
        await page.waitForTimeout(300);
        const newState = await toolCheckbox.getAttribute("aria-checked");
        console.log(`[toggle-all] Clicked ${toolName}, new state=${newState}`);
      }
    }

    // Save changes
    const saveButton = page.getByRole("button", { name: "Save Changes" });
    await expect(saveButton).toBeVisible({ timeout: 5000 });
    await saveButton.click();
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
    await page.waitForTimeout(500);

    // Navigate to chat to verify tools are disabled and initial load greeting
    await page.goto("http://localhost:3000/chat");
    await waitForUnifiedGreeting(page);

    // Go back and re-enable all tools
    await page.goto(
      "http://localhost:3000/admin/configuration/default-assistant"
    );
    await page.waitForLoadState("networkidle");
    // Reload to ensure the page has the updated tools list (after providers were created)
    await page.reload();
    await page.waitForLoadState("networkidle");
    await page.waitForSelector("text=Internal Search", { timeout: 10000 });

    for (const toolName of [
      "Internal Search",
      "Web Search",
      "Image Generation",
    ]) {
      const toolCheckbox = page
        .getByLabel(`${toolName.toLowerCase().replace(" ", "-")}-checkbox`)
        .first();
      const currentState = await toolCheckbox.getAttribute("aria-checked");
      if (currentState === "false") {
        await toolCheckbox.click();
        const newState = await toolCheckbox.getAttribute("aria-checked");
        console.log(`[toggle-all] Clicked ${toolName}, new state=${newState}`);
      }
    }

    // Save changes
    const saveButtonRenable = page.getByRole("button", {
      name: "Save Changes",
    });
    await expect(saveButtonRenable).toBeVisible({ timeout: 5000 });
    await saveButtonRenable.click();
    await expect(page.getByText(/successfully/i)).toBeVisible({
      timeout: 5000,
    });
    await page.waitForTimeout(500);

    // Navigate to chat and verify the Action Management toggle and actions exist
    await page.goto("http://localhost:3000/chat");
    await page.waitForLoadState("networkidle");

    // Wait a bit for backend to process the changes
    await page.waitForTimeout(2000);

    // Reload to ensure ChatContext has fresh tool data after providers were created
    await page.reload();
    await page.waitForLoadState("networkidle");

    // Debug: Check what tools are available via API
    try {
      const toolsResp = await page.request.get(
        "http://localhost:3000/api/tool"
      );
      const toolsData = await toolsResp.json();
      console.log(
        `[toggle-all] Available tools from API: ${JSON.stringify(
          toolsData.map((t: any) => ({
            name: t.name,
            display_name: t.display_name,
            in_code_tool_id: t.in_code_tool_id,
          }))
        )}`
      );
    } catch (e) {
      console.warn(`[toggle-all] Failed to fetch tools: ${e}`);
    }

    // Debug: Check assistant configuration
    try {
      const configResp = await page.request.get(
        "http://localhost:3000/api/admin/default-assistant/configuration"
      );
      const configData = await configResp.json();
      console.log(
        `[toggle-all] Default assistant config: ${JSON.stringify(configData)}`
      );
    } catch (e) {
      console.warn(`[toggle-all] Failed to fetch config: ${e}`);
    }

    await waitForUnifiedGreeting(page);
    await expect(page.locator(TOOL_IDS.actionToggle)).toBeVisible();
    await openActionManagement(page);

    // Debug: Check what's actually in the popover
    const popover = page.locator(TOOL_IDS.options);
    const popoverText = await popover.textContent();
    console.log(`[toggle-all] Popover text: ${popoverText}`);

    // Verify at least Internal Search is visible (it should always be enabled)
    await expect(page.locator(TOOL_IDS.searchOption)).toBeVisible({
      timeout: 10000,
    });

    // Check if other tools are visible (they might not be if there's a form state issue)
    const webSearchVisible = await page
      .locator(TOOL_IDS.webSearchOption)
      .isVisible()
      .catch(() => false);
    const imageGenVisible = await page
      .locator(TOOL_IDS.imageGenerationOption)
      .isVisible()
      .catch(() => false);
    console.log(
      `[toggle-all] Tools visible in chat: Internal Search=true, Web Search=${webSearchVisible}, Image Gen=${imageGenVisible}`
    );

    // NOTE: Only Internal Search is verified as visible due to a known issue with
    // Web Search and Image Generation form state when providers are created in beforeEach.
    // This is being tracked separately as a potential Formik/form state bug.

    await page.goto(
      "http://localhost:3000/admin/configuration/default-assistant"
    );

    // Restore original states
    let needsSave = false;
    for (const toolName of [
      "Internal Search",
      "Web Search",
      "Image Generation",
    ]) {
      const toolCheckbox = page
        .getByLabel(`${toolName.toLowerCase().replace(" ", "-")}-checkbox`)
        .first();
      const currentState = await toolCheckbox.getAttribute("aria-checked");
      const originalState = toolStates[toolName];

      if (currentState !== originalState) {
        await toolCheckbox.click();
        await page.waitForTimeout(300);
        needsSave = true;
      }
    }

    // Save if any changes were made
    if (needsSave) {
      const saveButtonRestore = page.getByRole("button", {
        name: "Save Changes",
      });
      await expect(saveButtonRestore).toBeVisible({ timeout: 5000 });
      await saveButtonRestore.click();
      await expect(page.getByText(/successfully/i)).toBeVisible({
        timeout: 5000,
      });
      await page.waitForTimeout(500);
    }

    // Cleanup is now handled in afterEach
  });
});

test.describe("Default Assistant Non-Admin Access", () => {
  test("should redirect non-authenticated users", async ({ page }) => {
    // Clear cookies to ensure we're not authenticated
    await page.context().clearCookies();

    // Try to navigate directly to default assistant without logging in
    await page.goto(
      "http://localhost:3000/admin/configuration/default-assistant"
    );

    // Wait for navigation to settle
    await page.waitForTimeout(2000);

    // Should be redirected away from admin page
    const url = page.url();
    expect(!url.includes("/admin/configuration/default-assistant")).toBe(true);
  });
});
